<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Date" %>
<%@ page import ="java.text.SimpleDateFormat" %>
<%@page import="java.text.DateFormat" %>
<%@ page import ="java.text.ParseException" %>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.services.AttendanceService" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.model.StudentAttendance" %>

<html>
<head>
<title>Edit Attendance</title>
<jsp:include page="../includeFiles.jsp" ></jsp:include>
<script src="<%=request.getContextPath()%>/public/js/validate.js"></script>

<link rel="stylesheet" href="<%=request.getContextPath()%>/views/css/ui/jquery-ui-1.10.4.custom.min.css">
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.core.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.widget.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.accordion.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.datepicker.js"></script>
<script>
	$(function() {
		$( "#date" ).datepicker({
			beforeShowDay: noSunday,
			dateFormat: 'yy-mm-dd'
		});
	});
	function noSunday(date){ 
	     return [date.getDay() != 0, ''];
	};
	/* If want to fix height inner content of accordion - 
		remove 'heightStyle' line (and the above comma too :p ).
	*/
	$(function() {
		$( "#accordion" ).accordion({
			collapsible: true,
			heightStyle: "content",
		});
	});
</script>

</head>

<body class="left-sidebar" onload="getValues()">
<% String currentUser = (String) request.getSession().getAttribute("currentUser");
	ArrayList<StudentAttendance> attendanceList = null;%>

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp"></jsp:include>
<jsp:include page="../common/checkTeacherRole.jsp" />

	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">
				
					<div id="login_content">
						<header>
							<h2>Edit Student Attendance</h2>
						</header>
						
							
					<span class="error_msg">${nullValues}</span>
					<span class="success_msg">${attendanceEditSuccess}</span>
					<span class="error_msg">${attendanceEditNotSuccess}</span>
				
					
						<!-- <form name="editAttendanceForm_1" action="" method="post"> -->
						<form name="editAttendanceForm_2" action="${pageContext.request.contextPath}/EditAttendanceServlet" method="post">
							<% 
								AddClassService addClassService = new AddClassService();							
								String classId = addClassService.getClassIdByTeacherId(currentUser); %>
								
							<input name="class_id" type="hidden" id="class_id" value="<%=classId%>">
							
							<div>
								<label>Date:</label>
								<input type="text" id="date" name="date" placeholder="Date">
							</div>
							
							<%  DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
								Date tempdate = null;
								java.sql.Date date=null;
								if(request.getParameter("date") != null) {
									
									try {
										tempdate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("date"));
									    date=new java.sql.Date(tempdate.getTime());		
									} catch (ParseException e) {
										e.printStackTrace();
									}
									AttendanceService attendanceService = new AttendanceService();
									//classId=request.getParameter("classId");
									attendanceList = attendanceService.getAttendanceListByDate_ClassId(classId, date);
								}
								%>
								<input name="date" type="hidden" id="date" value="<%=date%>">

								<% if(attendanceList != null ) {
									if(!attendanceList.isEmpty()) { %>
									<div id="accordion" style="clear:both;">
									<% request.getSession().setAttribute("attendanceList", attendanceList);
									  int i = -1;
										Iterator itr = attendanceList.iterator();
										StudentAttendance attendance = new StudentAttendance();
										while(itr.hasNext()) {
											i++;%>
											<h3> <% attendance = (StudentAttendance) itr.next(); %>
												<%= attendance.getStudent().getStud_fname() %>&nbsp;<%= attendance.getStudent().getStud_lname() %></h3>
										
												<div>
													<input type="hidden" value="<%= attendance.getStudent().getStud_id() %>" id="student_id" name="student_id">
													<input type="hidden" id="i_value" name="i_value" value=<%=i%>>
												
												
													<div id="dataForm">
														<label>Student Roll No: </label>
														<input type="text" id="student_roll_no" name="student_roll_no" value="<%=attendance.getStud_roll_no()%>" readonly="readonly">
													</div>
													
													<div id="dataForm">
														<label>Student Attendance Status Marked: </label>
														<input type="text" id="student_status" name="student_status" value="<%=attendance.getAttendance_status() %>" >
													</div>
												</div>
										<% } %>
										</div>
										  <div>
											 <input type="submit" value="Save Changes" class="button add_button">
										  </div>
										<% } else{ %>
											<div>
												<label> No attendance data found for this date.</label>
											</div>
										<% } %>
									
								<%}%>
							
							</form>
						</div>
				</div>
			</div>
		</div>
		
		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<%@include file="../common/sidebarHeader.jsp" %>

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<%@include file="../common/sidebarFooter.jsp" %>

		</div>
		
		<script type="text/javascript">
			$(function(){
				$("#editAttendace").addClass("current_page_item");
			});
			var contextPath='<%=request.getContextPath()%>';
		</script>
		<script type="text/javascript">
	
		$(function () {
		    var temp = 0;
		    $('#date').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var date = $("#date").val();
				setCookie("date",date);
				<%-- <% attendanceList = attendanceService.getAttendanceListByDate_ClassId(classId, date); %> --%>
				window.location.replace(contextPath + "/views/teacher/editAttendance.jsp?date=" + date);
		    });      
		});
		
		function getValues(){
			$("#date").val(getCookie("date"));
		}
		
		function getCookie(name) { 
			var re = new RegExp(name + "=([^;]+)"); 
			var value = re.exec(document.cookie);
			return (value != null) ? unescape(value[1]) : null; 
		}
		
		function setCookie(name, value) {
			  var today = new Date(); 
			  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
			  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
		 }
		
		function clearCookie(){
			deleteCookie("date");
			alert("Cookies Deleted");
		}
		
		function deleteCookie(name)
		{
			var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
			document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
		}
		
		</script>
	</div>
</body>
</html>